package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 执行关联查询
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        //王克晶的性别,以及所带的班级名称及楼层
        /**
          select t.name,t.gender,c.name,c.floor
          from teacher t
          join class c on t.id=c.teacher_id
          where t.name='王克晶'
         */
        try(Connection connection=DBUtil.getConnection();){
            Statement statement=connection.createStatement();
            String sql="select t.name,t.gender,c.name,c.floor " +
                    "from teacher t " +
                    "join class c on t.id=c.teacher_id " +
                    "where t.name='王克晶' ";
            ResultSet rs=statement.executeQuery(sql);
            while (rs.next()){
                String tName=rs.getString("t.name");
                String tGender=rs.getString("t.gender");
                String cName=rs.getString("c.name");
                int floor=rs.getByte("c.floor");
                System.out.println(tName +" "+tGender + " " + cName + " " + floor);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
